package com.learner.course.service.impl;

import com.learner.course.repository.CourseMarketRepository;
import com.learner.course.service.CourseMarketService;
import com.learner.pojo.course.CourseMarket;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;
/**
* @Description: 课程营销
* @Author: ice2020x
* @Date: 2021/8/22
*/
@Service
public class CourseMarketServiceImpl implements CourseMarketService {

    @Autowired
    CourseMarketRepository courseMarketRepository;

    @Override
    public CourseMarket getCourseMarketById(String courseId) {
        Optional<CourseMarket> optional = courseMarketRepository.findById(courseId);
        return optional.orElse(null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CourseMarket updateCourseMarket(String id, CourseMarket courseMarket) {
        CourseMarket one = this.getCourseMarketById(id);
        if(one!=null){
            one.setStartTime(courseMarket.getStartTime());
            one.setEndTime(courseMarket.getEndTime());
            one.setPrice(courseMarket.getPrice());
            one.setValid(courseMarket.getValid());
        }else{
            one = new CourseMarket();
            BeanUtils.copyProperties(courseMarket, one);
            one.setId(id);
        }
        courseMarketRepository.save(one);
        return one;
    }
}
